#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>

#include "kc031service.h"
#include "rader.h"
#include "ringBuffer.h"
#include "utils.h"

#ifdef USE_RADAR

static char raderBuf[4096*2];
static struct ringbuffer raderFifo;
static unsigned char rdaerCmdFlag = 0;
static unsigned short raderDlen = 0;

int rader_init()
{
	ringbuffer_init(&raderFifo,raderBuf,sizeof(raderBuf));
	return 0;
}

unsigned char rader_flag(char *cmd,int len)
{
	if(len < 1)return 0;
	if(cmd[0] == 'f') return DATA_FLAG_f;		//0x66
	if(cmd[0] == 'F') return DATA_FLAG_F;		//0x46
	if(cmd[0] == 'p') return DATA_FLAG_p;		//0x70
	if(cmd[0] == 'm') return DATA_FLAG_m;		//0x6D
	if(cmd[0] == 'M') return DATA_FLAG_M;		//0x4D
	if(cmd[0] == 'S') return DATA_FLAG_S;		//0x53
	return 0;
}

unsigned short rader_dlen(unsigned char flag)
{
	switch(flag)
	{
	case DATA_FLAG_f:
		return 3172;
	case DATA_FLAG_F:
		return 3178;
	case DATA_FLAG_p:
		return 1024;
	case DATA_FLAG_m:
		return 2144;
	case DATA_FLAG_M:
		return 64;
	case DATA_FLAG_S:
		return 10;
	default :
		return 0;
	}
}


unsigned char rader_recv(int fd,char *data,size_t len)
{
	char buf[4096];
	int ret = 0;
	
	if(raderDlen > 0 && ringbuffer_in(&raderFifo,data,len) >= raderDlen)
	{
		memset(buf,0,sizeof(buf));
		ERUP_SET_BEGIN(buf);
		ERUP_SET_LEN(buf,raderDlen);
		ERUP_SET_FLAG(buf,rdaerCmdFlag);
		ringbuffer_out(&raderFifo,ERUP_GET_DATA(buf),raderDlen);
		ERUP_SET_CRC(buf);
		ERUP_SET_END(buf);
		ret = write(fd,buf,ERUP_GET_PACKET_LEN(buf));
		//#ifdef PSU_DEBUG
		psus_hint("RADAR to TCP:%d---[RET=%d,%d]", ERUP_GET_PACKET_LEN(buf),ret,errno);
		//#endif
		if(ret == -1 ){
			ringbuffer_reset(&raderFifo);
			rdaerCmdFlag = 0;
			CloseTcpSocket(get_psus_data());
			StartTcpSocket(get_psus_data());
		}else{
			// if(rdaerCmdFlag == DATA_FLAG_F){
			// 	if(fok_for_timer(20,0)>0){
			// 		if(get_psus_data()->uart_fd > 0){
			// 			raderDlen = 0;  
			// 			rdaerCmdFlag = 0;
			// 			write(get_psus_data()->uart_fd,"F",1);
			// 			return 0;
			// 		}
			// 	}
			// }
		}
		
		raderDlen = 0;  
		ret = rdaerCmdFlag == DATA_FLAG_S ? DATA_FLAG_S:0;
		rdaerCmdFlag = 0;
	}else{
		//#ifdef PSU_DEBUG
		psus_hint("\tRADAR Ringbuffer[%d=>%d]",ringbuffer_len(&raderFifo),raderDlen);
		//#endif
	}
	
	return ret;
}


int rader_heart(int fd,char *buf,char *data,size_t len)
{
	ERUP_SET_BEGIN(buf);
	ERUP_SET_LEN(buf,len);
	ERUP_SET_FLAG(buf,0);
	memcpy(ERUP_GET_DATA(buf),data,len);
	ERUP_SET_CRC(buf);
	ERUP_SET_END(buf);
	return ERUP_GET_PACKET_LEN(buf);
}

unsigned char rader_cmd(int fd,unsigned char *buf,size_t len)
{
	/*if(ERUP_GET_END(p) != ERUP_END || ERUP_GET_CRC(p) != crc8(ERUP_GET_DATA(p),ERUP_GET_LEN(p))){
		psus_error("radar cmd packnet error.\n");
		return 0; //packnet error 
	}*/
	
	if(ERUP_GET_LEN(buf) >=13)
	{
		if(strcasecmp(ERUP_GET_DATA(buf),"close socket!") == 0)
		{
			psus_hint("receive close socket command!");
			rdaerCmdFlag = 0;
			raderDlen = 0;
			return 1;
		}
	}else{
		if(raderDlen == 0)
		{
			ringbuffer_reset(&raderFifo);
			rdaerCmdFlag = rader_flag(ERUP_GET_DATA(buf),ERUP_GET_LEN(buf));
			raderDlen = rader_dlen(rdaerCmdFlag);
			//psus_hint("Write to %d :%02X[%d]\n",fd,ERUP_GET_DATA(buf)[0],raderDlen);
			//if(rader_flag != DATA_FLAG_F){
				write(fd,ERUP_GET_DATA(buf),ERUP_GET_LEN(buf));
			//}
		}else{
			psus_hint("RADAR CMD[%02X]:data from uart not finish [%d]\n",rdaerCmdFlag,raderDlen);
		}
	}
	return 0;
}

void rader_cmd_from_stdin(int fd,char *buf,size_t len)
{
	char *p = buf;
	
	if(len >= 3)
	{
		if(strcasecmp(buf,"cls") == 0)
		{
			rdaerCmdFlag = 0;
			raderDlen = 0;
			return;
		}else{
			if(buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X'))
			{
				p[0] = (char)(StrToInt(buf) && 0xFF);
				len = 1;
			}
		}
	}else{
		if(raderDlen == 0)
		{
			ringbuffer_reset(&raderFifo);
			rdaerCmdFlag = rader_flag(p,len);
			raderDlen = rader_dlen(rdaerCmdFlag);
			write(fd,p,len);
		}else{
			printf("receive data is not finish from stdin \n");
		}
	}
	
}

void set_raderDlen(unsigned short value)
{
	raderDlen = value;
}

#endif
